package com.layui.ad.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.layui.ad.annotation.DataSource;
import com.layui.ad.constant.DictConstant;
import com.layui.ad.entity.Article;
import com.layui.ad.enums.DataSourceEnum;
import com.layui.ad.mapper.ArticleMapper;
import com.layui.ad.query.ArticleQuery;
import com.layui.ad.service.ArticleService;
import com.layui.ad.utils.*;
import com.layui.ad.vo.ArticleListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public JsonResult getList(ArticleQuery articleQuery){
        PageHelper.startPage(articleQuery.getPage(),articleQuery.getLimit());
        List<Article> articleList = articleMapper.getList(articleQuery);
//        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
//        if(!StringUtils.isEmpty(articleQuery.getTitle())){
//            queryWrapper.like("title",articleQuery.getTitle());
//        }
        PageInfo<Article> pageInfo = new PageInfo<Article>(articleList);
        List<Article> articlePageList = pageInfo.getList();
        List<ArticleListVo> articleListVoList = new ArrayList<>();
        if(articlePageList!=null){
            articlePageList.forEach(item->{
                ArticleListVo articleListVo = new ArticleListVo();
                BeanUtils.copyProperties(item,articleListVo);
                articleListVo.setCreateUserName(AdminUtils.getAdminName(item.getCreateUser()));
                articleListVo.setTopName(DictConstant.TOP_LIST.get(item.getIsTop()));
                articleListVo.setStatusName(DictConstant.ARTICLE_STATUS_LIST.get(item.getStatus()));
                articleListVo.setUpdateUserName(AdminUtils.getAdminName(item.getUpdateUser()));
                articleListVoList.add(articleListVo);
            });
        }

        return JsonResult.ok("操作成功",articleListVoList,pageInfo.getTotal());
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult edit(Article article){
        if(article.getId()>0){
            article.setUpdateTime(DateUtils.now());
            article.setUpdateUser(ShiroUtils.getAdminId());
            this.updateById(article);
        }else {
            article.setCreateTime(DateUtils.now());
            article.setCreateUser(ShiroUtils.getAdminId());
            this.save(article);
        }
        return JsonResult.ok();
    }

    @Override
    public Map<String,String> info(Integer id){
        Object obj = articleMapper.info(id);
        Map<String, String> map = JSON.parseObject(JSON.toJSONString(obj), new TypeReference<Map<String, String>>() {
        });
        return map;
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult delete(Integer id){
        if(id==null || id==0){
            return JsonResult.fail("参数不正确");
        }

        Object obj = articleMapper.selectById(id);
        if(obj == null){
            return JsonResult.fail("没有查询到数据");
        }
        if(articleMapper.deleteById(id)>0){
            return JsonResult.ok();

        }else{
            return JsonResult.fail("删除失败");

        }
    }

    @Override
    public JsonResult batchDelete(String ids){
        String[] str = ids.split(",");
        Integer num = articleMapper.deleteBatchIds(Arrays.asList(str));
        if(num>0) {
            return JsonResult.ok();
        }else{
            return JsonResult.fail();
        }
    }
}
